package com.luoqy.speedy.core.generator.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.luoqy.speedy.core.generator.config.CodeConfig;
import com.luoqy.speedy.core.generator.util.CodeGenerate;
import com.luoqy.speedy.data.MySql;
import com.luoqy.speedy.util.Result;
import com.luoqy.speedy.util.aop.log.Log;
/**
 * @author luoqy
 * @date 2019年7月10日
 * 代码生成控制类
 */
@Controller
@RequestMapping("/code")
public class CodeController {
	@RequestMapping("")
	public String index(Model model){
		//这里加载所有的表和注释，
		Object tables=MySql.SELECT("SELECT TABLE_NAME AS tablename,TABLE_COMMENT AS comment FROM information_schema. TABLES WHERE table_type = 'BASE TABLE' AND table_schema = DATABASE () and TABLE_NAME not like 'speedy%'; ", "list",null);
		model.addAttribute("tables", tables);
		return "WEB-INF/code/code";
	}
	@RequestMapping("/generate")
	@ResponseBody
	@Log({"生成页面","选择表生成需要的页面文件"})
	public Result generate(CodeConfig condeConig){
		String path=condeConig.getRequestPath();
		String nameValue=condeConig.getBizName();
		String sql="select  id from speedy_menu where path='"+path+"' or name='"+nameValue+"'";
		Object menu=MySql.SELECT(sql, "none",null);
		if(null!=menu){
			return Result.error("已存在的请求");
		}
		String moduleName=condeConig.getTablename().replace(condeConig.getIgnoreTabelPrefix(), "");
		condeConig.setModuleName(moduleName);
		if(null==condeConig.getProjectPackage()||"".equals(condeConig.getProjectPackage())){
			condeConig.setProjectPackage("com.luoqy.speedy.modular.system");
		}
		if(CodeGenerate.codeGenerate(condeConig)){
			String bizname=condeConig.getBizName();
			String tablename=condeConig.getTablename();
			String insertSql="select  id from speedy_menu where path='"+path+"' or name='"+nameValue+"'";
			return Result.success("生成成功",null);
		}else{
			return Result.error("生成失败");
		}
		
	}
}
